package com.wngbms;

import com.atomikos.icatch.jta.UserTransactionImp;
import com.atomikos.jdbc.AtomikosDataSourceBean;

import javax.transaction.NotSupportedException;
import javax.transaction.SystemException;
import javax.transaction.UserTransaction;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;

public class AtomikosDemoProcess1 {

    public static void main(String[] args) throws SQLException, SystemException, NotSupportedException {

        Properties p = new Properties();
        p.setProperty("url", "jdbc:mysql://localhost:3306/" + "db_user");
        p.setProperty("user", "root");
        p.setProperty("password", "password");

        AtomikosDataSourceBean ds = new AtomikosDataSourceBean();
        ds.setUniqueResourceName("db_user");
        ds.setXaDataSourceClassName("com.mysql.cj.jdbc.MysqlXADataSource");
        ds.setXaProperties(p);

        UserTransaction userTransaction = new UserTransactionImp();
        userTransaction.begin();

        Connection connection = ds.getConnection();
        PreparedStatement preparedStatement = connection.prepareStatement("INSERT into user(name) VALUES (?)", Statement.RETURN_GENERATED_KEYS);
        preparedStatement.setString(1, "wngbms");
        preparedStatement.executeUpdate();




    }
}
